﻿/*
*  全局变量
*/
var popMenu = void (0);
var eagleeyeControl = void (0);
var levelcontrol = void (0);
var dswitcher = void (0);
var ishide = false;
var scaleControl = void (0);
var dMap = void (0); //主界面的地图
var isAddedgeClick = false;//点击添加区域状态
var xmlHttp; //XML对象
var edgepolygon = null;//添加边界区域多边形
var alledgeCoord="";

//入口函数
function MonitorMapLoad() {
    //从XML读取 用户设置的刷新频率  UpdateInterval
    initialMaps(); //首先实现基础GIS功能

}

/**
* 整个地图全部初始化,最终功能是要求呈现出基础地图功能。不包括标记,该函数只能调用一次,并且是在地图第一次load的时候调用
*/
function initialMaps() {
    if (dMap == null)
        dMap = new DMap(document.getElementById("mapcontainer"));
    var icenter = new DLonLat(118.5, 36.5);
    var izoom = 12;
    dMap.setCenter(icenter, izoom);
    dMap.enableScrollWheelZoom();
    dMap.addLayer(D_OVERLAY_TDT_VECTOR_LAYER);
    dMap.addLayer(D_OVERLAY_VECTOR_LAYER);
    dMap.addLayer(D_OVERLAY_DY_RASTER_LAYER);
    D_OVERLAY_DY_RASTER_LAYER.display(false);
    dMap.addLayer(D_OVERLAY_TDT_RASTER_LAYER);
    D_OVERLAY_TDT_RASTER_LAYER.display(false);
    dMap.addLayer(D_OVERLAY_DY_VECTOR_LAYER);
    //显示地图中心点坐标
    showMapCenterPosition();
    showScale();
    showLevel();
    showEagleEye();
    setKeyEnable();

    dMap.addListener("mousemove", showMousePosition, this);
    dMap.addListener("dragend", showMapCenterPosition, this);

    drawedge();
}



//键盘控制地图
function setKeyEnable() {
    dMap.enableKeyMap();
}

//显示鹰眼控件
function showEagleEye() {
    if (!eagleeyeControl) {
        eagleeyeControl = new DEagleeye();
        dMap.addControl(eagleeyeControl);
        eagleeyeControl.open();
    }
    if (eagleeyeControl) {
        eagleeyeControl.show();
    }
}

//缩放等级工具
function showLevel() {
    if (!levelcontrol) {
        levelcontrol = new DLevel({ select: 4, OffsetX: 20, pos: DControlAnchor.TOP_LEFT, top: 0 });
        dMap.addControl(levelcontrol);
    }
    if (levelcontrol) {
        levelcontrol.show();
    }
}


//显示比例尺控件
function showScale() {
    if (!scaleControl) {
        scaleControl = new DScale();
        dMap.addControl(scaleControl);
    }
    if (scaleControl) {
        scaleControl.show();
    }
}
//切换底图状态

function addYX() {
    dMap.setMapType(TDT_SATE_MAP);
    D_OVERLAY_VECTOR_LAYER.display(false);
    D_OVERLAY_TDT_RASTER_LAYER.display(true);
    D_OVERLAY_TDT_VECTOR_LAYER.display(false);
    D_OVERLAY_DY_VECTOR_LAYER.display(false);
    D_OVERLAY_DY_RASTER_LAYER.display(true);

}
//移除底图切换控件
function addSL() {
    dMap.setMapType(TDT_VECTOR_MAP);
    D_OVERLAY_VECTOR_LAYER.display(true);
    D_OVERLAY_TDT_RASTER_LAYER.display(false);
    D_OVERLAY_TDT_VECTOR_LAYER.display(true);
    D_OVERLAY_DY_VECTOR_LAYER.display(true);
    D_OVERLAY_DY_RASTER_LAYER.display(false);
}

/*
*显示地鼠标坐标函数
*/
function showMousePosition(p) {
    if (p == null) {
        return;
    }

    var Bstr = "";
    var Lstr = "";
    if (p.lat < 0) {
        var latTemp = 0 - p.lat;
        Bstr = "<span class=\"tipsN\">S</span>" + Rad2Deg(latTemp);
    }
    else
        Bstr = "<span class=\"tipsN\">N</span>" + Rad2Deg(p.lat);
    if (p.lon < 0) {
        var lonTemp = 0 - p.lon;
        Lstr = "<span class=\"tipsN\">W</span>" + Rad2Deg(lonTemp);
    }
    else
        Lstr = "<span class=\"tipsN\">E</span>" + Rad2Deg(p.lon);
    document.getElementById("mousePosition").innerHTML = "坐标:<span class=\"tipsShuzi\">" + Bstr + "</span> <span class=\"tipsShuzi\">" + Lstr + "</span>";
}


/*
*显示地图中心坐标函数
*/
function showMapCenterPosition() {
    var center = dMap.getCenter();
    var Bstr = "";
    var Lstr = "";
    if (center.lat < 0) {
        var latTemp = 0 - center.lat;
        Bstr = "<span class=\"tipsN\">S</span>" + Rad2Deg(latTemp);
    }
    else
        Bstr = "<span class=\"tipsN\">N</span>" + Rad2Deg(center.lat);
    if (center.lon < 0) {
        var lonTemp = 0 - center.lon;
        Lstr = "<span class=\"tipsN\">W</span>" + Rad2Deg(lonTemp);
    }
    else
        Lstr = "<span class=\"tipsN\">E</span>" + Rad2Deg(center.lon);
    document.getElementById("centerPosition").innerHTML = "中心:<span class=\"tipsShuzi\">" + Bstr + "</span> <span class=\"tipsShuzi\">" + Lstr + "</span>";
}

//function getcord() {
//    dMap.setMouseMode(DMouseMode.GET_POINT, function(ee) {
//        var x = document.getElementById("coordx");
//        var y = document.getElementById("coordy");
//        x.value = Rad2Deg(ee.toString().split(",")[0]);
//        y.value = Rad2Deg(ee.toString().split(",")[1]);
//        dMap.setMouseMode(DMouseMode.MAP_DRAG);
//    });
//    getcord();
//}

/*
* 弧度转换为角度
*/

function Rad2Deg(deg) {
    var d = parseInt(deg);
    var f = parseInt((deg - d) * 60);
    var m = parseInt(((deg - d) * 60 - f) * 60);
    if (f.toString().length == 1) f = "0" + f;
    if (m.toString().length == 1) m = "0" + m;
    return d + "° " + f + "\' " + m + "\" ";
}

function drawedge() {

    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
    xmlHttp.onreadystatechange = edgehandle;
    xmlHttp.open("GET", "GetAreaedge.aspx?r=", true);
    xmlHttp.send(null);
}


function edgehandle() {
    var xmlDom;
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            xmlDom = xmlHttp.responseXML;
            var Areaedges = xmlDom.documentElement.getElementsByTagName("Areaedge");
            if (Areaedges[0] == null) {
                alert("无合法区域信息");
                return false;
            }
            var polygon = null;
            for (var bn = 0; bn < Areaedges.length; bn++) 
            {
                var edgeCor = Areaedges[bn].getAttribute("edgeCor");
                polygon=new DPolygon(edgeCor, { color: "red", weight: 2, opacity: 0.8 });
                dMap.addOverlay(polygon);
                if (bn == 0)
                    alledgeCoord = alledgeCoord + edgeCor;
                else
                    alledgeCoord = alledgeCoord +","+ edgeCor;
            }
        }
    }
}


function addAreaedge() {

    isAddedgeClick = !isAddedgeClick;
    if (isAddedgeClick)  //点击的时候
    {
        document.getElementById("btnAddedge").innerHTML = "取消";
        document.getElementById("btnAddedge").style.fontWeight = 'bold';
        document.getElementById("edgeCorInfo").style.display = 'none';

        dMap.setMouseMode(DMouseMode.DRAW_POLYGON,
        function(strCoord) {
            /**
            *回调回来的是线经纬度坐标串，用户用来定义先对象，添加到地图上
            **/
            edgepolygon = null;
            dMap.setMouseMode(DMouseMode.MAP_DRAG);
            if (!edgepolygon) {
                //定义多边形对象
                edgepolygon = new DPolygon(strCoord, { color: "red", weight: 2, opacity: 0.8 });
                //地图添加线对象
                dstrCoord = strCoord.split(",");
                dalledgeCoord = alledgeCoord.split(",");
                var edgeCor = "";
                var i = 0;
                var j = 0;
                var lon1 = new Array();
                var lat1 = new Array();
                var lon2 = new Array();
                var lat2 = new Array();
                var length1 = dstrCoord.length / 2;
                var length2 = dalledgeCoord.length / 2;
                for (i = 0; i < length1; i++) {
                    lon1[i] = parseFloat(dstrCoord[2 * i]);
                    lat1[i] = parseFloat(dstrCoord[(2 * i + 1)]);
                    var cc1 = new DLonLat(lon1[i], lat1[i]);
                    for (j = 0; j < length2; j++) {
                        lon2[j] = parseFloat(dalledgeCoord[2 * j]);
                        lat2[j] = parseFloat(dalledgeCoord[(2 * j + 1)]);
                        var cc2 = new DLonLat(lon2[j], lat2[j]);
                        var len = 0;
                        var d = DLonLat.getDistanceOfTwo(cc1, cc2);
                        len = d + len;
                        dresult = Math.round(len) / 1000;
                        if (dresult < 5)
                            cc1 = cc2;
                    }
                    if (i == 0)
                        edgeCor = edgeCor + cc1.toString(); //最后一个坐标不需要逗号分隔
                    else
                        edgeCor = edgeCor + "," + cc1.toString();
                }
                edgepolygon = new DPolygon(edgeCor, { color: "red", weight: 2, opacity: 0.8 });
                //添加多边形对象到地图上
                dMap.addOverlay(edgepolygon);
            }
            document.getElementById("edgeCorInfo").value =edgeCor;
            //            document.getElementById("edgeCorInfo").innerHTML = "合法区域坐标：" + alledgeCoord;
        });
    }
    else {
        document.getElementById("btnAddedge").innerHTML = "手动添加合法区域";
        document.getElementById("btnAddedge").style.fontWeight = 'normal';
        document.getElementById("edgeCorInfo").style.display = 'none';
        document.getElementById("Errortips").innerHTML = "";
        document.getElementById("edgeCorInfo").value = "";
        dMap.removeOverlay(edgepolygon);
    }
}
    
        
                   



